package com.example.ck.utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teautil.models.*;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

public class AliSmsUtil {
    private static Client client;

    static {
        Config config = new Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId("LTAI5tPzdhxszDnNzk1eWBMZ")
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret("sxmXkVsgZdqzoY5RAWgfBvEG9VHXO1");
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param phone 手机号
     * @param code  验证码
     * @return
     */
    public static int sendSmsCode(String phone, String code) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("城科实验室申报系统")
                .setTemplateCode("SMS_465324470")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":" + code + "}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            return response.getStatusCode();
        } catch (Exception e) {

        }
        return -1;
    }

    /**
     * @param phone 手机号
     * @param name  用户名
     * @return
     */
    public static int sendSmsCreate(String phone, String name) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName("城科实验室申报系统")
                .setTemplateCode("SMS_465354418")
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"name\":" + name + "}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            return response.getStatusCode();
        } catch (Exception e) {

        }
        return -1;
    }
}
